﻿(function () {

    layui.use(["layer", "jquery", "form", "laydate", "tree"], function () {
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var laydate = layui.laydate;
        var tree = layui.tree;
        var userId = 0;

        $(document).ready(function () {
            loadShops();
            $("#btn-Shop-Search-Search").click(on_Shop_Search_SearchBtn_Click);
            $("#btn-Shop-Search-NewShop").click(on_Shop_Search_NewShopBtn_Click);
        });

        function on_Shop_Search_SearchBtn_Click() {
            loadShops();
        }

        function loadShops() {
            $("#ShopList").html("");
            var loadingIndex = layer.msg(addStyle("正在加载 ......"),
                { icon: 16, shade: 0.01, offset: ["25%"], time: false });
            $.ajax("/SystemManage/Shop/QueryByWhere", {
                method: "POST",
                data: {
                    Name: $("#txt-Shop-Search-ShopName").val()
                },
                complete: function (jqXHR, textStatus) {
                    layer.close(loadingIndex);
                },
                success: function (data, textStatus, jqXHR) {
                    if (data.Success == true) {
                        handleData(data.Data);
                    } else {
                        layer.msg(addStyle("查询店铺数据失败: " + data.Msg));
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(addStyle("系统错误，请联系管理员！"),
                        { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                }
            });

            function handleData(data) {
                var html = $("#ShopListTableDlg").html();
                data.forEach(item => {
                    var html2 = html;
                    html2 = html2.replace("{{ShopName}}", item["ShopName"]);
                    html2 = html2.replace("{{ShopManager}}", item["ShopManager"]);
                    html2 = html2.replace("{{ShopNo}}", item["ShopNo"]);
                    html2 = html2.replace("{{CreateTime}}", item["CreateTime"]);
                    html2 = html2.replace("{{ShopManagerUserId}}", item["ShopManagerUserId"]);
                    $("#ShopList").append(html2);
                    $("#ShopList a.view-ShopUsers:last").bind("click", item, on_ShopListTableDlg_ViewShopUsersBtn_Click);
                });
            }
        }

        function on_Shop_Search_NewShopBtn_Click() {
            var layerId = layer.open({
                type: 1,
                title: "满堂创品",
                shade: [0.5, '#000'],
                skin: 'layui-layer-molv',
                area: ["90%", "300px"],
                offset: "10%",
                resize: false,
                content: $("#NewShopDlg").html(),
                success: function (layero, index, that) {
                    $("#btn-NewShopDlg-Search").click(on_NewShopDlg_SearchBtn_Click);
                    $("#btn-NewShopDlg-OK").click(on_NewShopDlg_OKBtn_Click);

                    form.verify({
                        shopName: [/^[a-zA-Z0-9\u4e00-\u9fa5]{3,10}$/, "店铺名称格式不正确！"]
                        , shopNo: [/^[a-zA-Z0-9]{6,15}$/, "店铺编号格式不正确！"]
                    });
                }
            });

            function on_NewShopDlg_SearchBtn_Click() {
                var success = form.validate("#txt-NewShopDlg-Phone");
                if (success) {
                    var loadingIndex = layer.msg(addStyle("正在加载 ......"),
                        { icon: 16, shade: 0.01, offset: ["25%"], time: false });
                    $.ajax("/SystemManage/User/QueryByPhone", {
                        method: "POST",
                        data: "Phone=" + $("#txt-NewShopDlg-Phone").val(),
                        complete: function (jqXHR, textStatus) {
                            layer.close(loadingIndex);
                        },
                        success: function (data, textStatus, jqXHR) {
                            if (data.Success == true) {
                                handleData(data.Data);
                            } else {
                                layer.msg(addStyle("搜索会员数据失败: " + data.Msg));
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.msg(addStyle("系统错误，请联系管理员！"),
                                { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                        }
                    });
                }

                function handleData(data) {
                    if (data["Id"] > 0) {
                        userId = data["Id"];
                        var text = data["Name"] + " - " + data["InvitationCode"];
                        $("#lb-NewShopDlg-NewShopManager").text(text);
                    } else {
                        layer.msg(addStyle("未搜索到会员！"));
                        userId = 0;
                        $("#lb-NewShopDlg-NewShopManager").text("未选择！");
                    }
                }
            }

            function on_NewShopDlg_OKBtn_Click() {
                if (userId == 0) {
                    layer.msg(addStyle("请先选择新店长！"));
                    return;
                }

                if (!form.validate("#txt-NewShopDlg-ShopName")) {
                    return;
                }

                if (!form.validate("#txt-NewShopDlg-ShopNo")) {
                    return;
                }

                layer.confirm(addStyle("请仔细核对新店长的身份信息，确认无误吗？")
                    , { title: "满堂创品", icon: 3 }
                    , function (index) {
                        layer.close(index);

                        var loadingIndex = layer.msg(addStyle("正在处理 ......"),
                            { icon: 16, shade: 0.01, offset: ["25%"], time: false });
                        $.ajax("/SystemManage/Shop/NewShop", {
                            method: "POST",
                            data: {
                                ShopManagerUserId: userId,
                                ShopName: $("#txt-NewShopDlg-ShopName").val(),
                                ShopNo: $("#txt-NewShopDlg-ShopNo").val()
                            },
                            complete: function (jqXHR, textStatus) {
                                layer.close(loadingIndex);
                            },
                            success: function (data, textStatus, jqXHR) {
                                if (data.Success == true) {
                                    layer.msg(addStyle("开出新店铺成功！"));
                                    layer.close(layerId);
                                    loadShops();
                                } else {
                                    layer.msg(addStyle("开出新店铺失败: " + data.Msg));
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.msg(addStyle("系统错误，请联系管理员！"),
                                    { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                            }
                        });
                    }
                );
            }
        }

        function on_ShopListTableDlg_ViewShopUsersBtn_Click(event) {
            var shopManagerUserId = event.data["ShopManagerUserId"];
            layer.open({
                type: 1,
                title: "满堂创品",
                shade: [0.5, '#000'],
                skin: 'layui-layer-molv',
                area: ["90%", "90%"],
                offset: "5%",
                resize: false,
                content: $("#ShopUsersDlg").html(),
                success: function (layero, index, that) {
                    window.setTimeout(function () {
                        loadShopUsers();
                    }, 0.5 * 1000);

                    $("#btn-ShopUsersDlg-Search").click(on_ShopUsersDlg_SearchBtn_Click);
                }
            });

            function on_ShopUsersDlg_SearchBtn_Click() {
                loadShopUsers();
            }

            function loadShopUsers() {
                $("#ShopUsersList").html("");
                var loadingIndex = layer.msg(addStyle("正在加载 ......"),
                    { icon: 16, shade: 0.01, offset: ["25%"], time: false });
                $.ajax("/SystemManage/Shop/QueryShopUsersByShopManagerUserId", {
                    method: "POST",
                    data: {
                        ShopManagerUserId: shopManagerUserId,
                        Name: $("#txt-ShopUsersDlg-Name").val(),
                        Phone: $("#txt-ShopUsersDlg-Phone").val()
                    },
                    complete: function (jqXHR, textStatus) {
                        layer.close(loadingIndex);
                    },
                    success: function (data, textStatus, jqXHR) {
                        if (data.Success == true) {
                            handleData(data.Data);
                        } else {
                            layer.msg(addStyle("查询店铺会员数据失败: " + data.Msg));
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.msg(addStyle("系统错误，请联系管理员！"),
                            { icon: 2, time: 3 * 1000, offset: ["25%"], closeBtn: true });
                    }
                });

                function handleData(data) {
                    var html = $("#ShopUserListTableDlg").html();
                    data.forEach(item => {
                        var html2 = html;
                        html2 = html2.replace("{{Name}}", item["Name"]);
                        html2 = html2.replace("{{InvitationCode}}", item["InvitationCode"]);
                        html2 = html2.replace("{{ReferenceInvitationCode}}", item["ReferenceInvitationCode"]);
                        html2 = html2.replace("{{Phone}}", item["Phone"]);
                        html2 = html2.replace("{{Reference}}", item["Reference"]);
                        $("#ShopUsersList").append(html2);
                    });
                }
            }
        }

        function addStyle(str) {
            return '<span style="font-size: 16px;">' + str + '</span>';
        }
    });

})();